/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2021并保留所有权利。
 * Copyright 2021, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.www.cnblogs.com.junma.com.zte.zoo.keeper.configuration;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

import java.util.Random;

public class Controller {
    public static void main(String[] args) throws Exception {
        CuratorFramework newClient = CuratorFrameworkFactory.newClient("172.16.7.141:2181", new ExponentialBackoffRetry(1000, 3));
        newClient.start();
//        for (int i = 0; i <3 ; i++) {
            Random r = new Random();
        ConfigBean configBean = new ConfigBean()
                .setUrl("172.16.16.128")
                .setPort(r.nextInt()*100+"")
                .setName("majun")
                .setPwd("123");
//        createNode(newClient, "/conf", CreateMode.PERSISTENT, configBean.toString());


            System.out.println("###"+r.nextInt()*100);
            updateNodeDate(newClient,"/conf",configBean.setName("xiaoya--"+"##"+r.nextDouble()).toString());
//        }

    }
    public static void createNode(CuratorFramework newClient, String path, CreateMode createMode, String data) {
        try {
            newClient.create().withMode(createMode).forPath(path, data.getBytes());
        } catch (Exception e) {
            System.out.println("创建节点失败, elog=" + e.getMessage());
        }
    }
    public static void updateNodeDate(CuratorFramework newClient, String path, String data) {
        try {
            newClient.setData().forPath(path, data.getBytes());
        } catch (Exception e) {
            System.out.println("更新节点数据失败, elog=" + e.getMessage());
        }
    }

}